package com.ruoyi.internet.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.internet.api.domain.IntUser;
import com.ruoyi.internet.api.enums.IntUserEnum;
import com.ruoyi.internet.domain.IntStorage;
import com.ruoyi.internet.mapper.IntStorageMapper;
import com.ruoyi.internet.mapper.IntUserMapper;
import com.ruoyi.internet.service.IIntStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仓储物流Service业务层处理
 *
 * @author cjie
 * @date 2021-08-16
 */
@Service
public class IntStorageServiceImpl implements IIntStorageService {
    @Autowired
    private IntStorageMapper intStorageMapper;
    @Autowired
    private IntUserMapper intUserMapper;

    /**
     * 查询仓储物流
     *
     * @param id 仓储物流ID
     * @return 仓储物流
     */
    @Override
    public IntStorage selectIntStorageById(String id) {
        IntStorage intStorage = intStorageMapper.selectIntStorageById(id);
        IntUser intUser = intUserMapper.selectIntUserById(intStorage.getExpirationUserId());
        if(IntUserEnum.企业交易商.getCode().equals(intUser.getType())){
            intStorage.setExpirationName(intUser.getEnterpriseName());
        }
        intStorage.setType(intUser.getType());
        //未登录，去掉联系人和电话
        if (StringUtils.isEmpty(SecurityUtils.getUsername())) {
            intStorage.setLinkMan(null);
            intStorage.setPhone(null);
        }
        if(StringUtils.isNotEmpty(intStorage.getGoodsPic())){
            intStorage.setGoodsPicArray(JSON.parseArray(intStorage.getGoodsPic()));
        }
        return intStorage;
    }

    /**
     * 查询仓储物流列表
     *
     * @param intStorage 仓储物流
     * @return 仓储物流
     */
    @Override
    public List<IntStorage> selectIntStorageList(IntStorage intStorage) {
        List<IntStorage> intStorages = intStorageMapper.selectIntStorageList(intStorage);
        intStorages.forEach(storage->{
            if(StringUtils.isNotEmpty(storage.getGoodsPic())){
                storage.setGoodsPicArray(JSON.parseArray(storage.getGoodsPic()));
            }
        });
        return intStorages;
    }

    /**
     * 新增仓储物流
     *
     * @param intStorage 仓储物流
     * @return 结果
     */
    @Override
    public int insertIntStorage(IntStorage intStorage) {
        intStorage.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        intStorage.setCreateTime(DateUtils.getNowDate());
        intStorage.setCreateBy(SecurityUtils.getUsername());
        intStorage.setExpirationUserId(SecurityUtils.getUserId());

        if(intStorage.getGoodsPicArray() != null){
            intStorage.setGoodsPic(JSON.toJSONString(intStorage.getGoodsPicArray()));
        }
        return intStorageMapper.insertIntStorage(intStorage);
    }

    /**
     * 修改仓储物流
     *
     * @param intStorage 仓储物流
     * @return 结果
     */
    @Override
    public int updateIntStorage(IntStorage intStorage) {
        intStorage.setUpdateTime(DateUtils.getNowDate());
        intStorage.setUpdateBy(SecurityUtils.getUsername());
        if(intStorage.getGoodsPicArray() != null){
            intStorage.setGoodsPic(JSON.toJSONString(intStorage.getGoodsPicArray()));
        }
        return intStorageMapper.updateIntStorage(intStorage);
    }

    /**
     * 批量删除仓储物流
     *
     * @param ids 需要删除的仓储物流ID
     * @return 结果
     */
    @Override
    public int deleteIntStorageByIds(String[] ids) {
        return intStorageMapper.deleteIntStorageByIds(ids);
    }

    /**
     * 删除仓储物流信息
     *
     * @param id 仓储物流ID
     * @return 结果
     */
    @Override
    public int deleteIntStorageById(String id) {
        return intStorageMapper.deleteIntStorageById(id);
    }
}
